package cn.kgc.spring.day0302.factory;

import com.mysql.jdbc.Driver;
import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author 课工场
 * @date 2024/7/15
 * @description   获取Connection连接对象的类
 */

// @Component("connection")
public class ConnectionFactory implements FactoryBean {

    //  实现了FactoryBean的类 在哪托管到spring容器之后  获取对象时会调用getObject()
    // 将getObject()方法的返回值 作为容器中的对象进行返回
    @Override
    public Object getObject() throws Exception {

        System.out.println("------------getObject()----------------");
        // 如何返回一个连接对象
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);

        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/java2425";
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }
}
